package com.gemme.gemme.login;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectChangeListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.gemme.gemme.BaseActivity;
import com.gemme.gemme.R;
import com.gemme.gemme.util.DpUtil;
import com.gemme.gemme.util.SPUtil;
import com.gemme.gemme.util.ShapeUtil;
import com.gemme.gemme.view.InterestView;
import com.gemme.gemme.view.MyButton;
import com.gemme.gemme.view.SexView;
import com.gemme.gemme.view.TvTvArrow;
import com.jaeger.library.StatusBarUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

//侧滑删除地址
public class SexAgeActivity extends BaseActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sex_age);
        StatusBarUtil.setTransparent(this);
        initView();
        initTimePicker();
        getIntentData();
    }
    int resquest;
    private void getIntentData() {
        try {

            resquest = getIntent().getIntExtra("data", -1);
        }catch (Exception e){

        }
    }

    SexView man_sexview;
    SexView woman_sexview;
    String user_birthDay;
    MyButton button1;
    TvTvArrow select_brith_day;
    private void initView() {
        man_sexview = findViewById(R.id.man_sexview);
        woman_sexview = findViewById(R.id.woman_sexview);
        man_sexview.setImage(R.drawable.pc_man1);
        woman_sexview.setImage(R.drawable.pc_woman1);
        button1 = findViewById(R.id.button1);
        setSexClick();
//        tv_vip.setBackground(ShapeUtil.getCornBg(DpUtil.dp2px(mContext,9),"#FF403F3F"));

//        button1.setBackground(ShapeUtil.getCornBg(DpUtil.dp2px(mContext,4),"#ffe6e6e6"));
        button1.setSatate(false);
        select_brith_day = findViewById(R.id.select_brith_day);
        select_brith_day.setLeftText("选择生日").setRightText("仅自己可见");
        select_brith_day.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pvTime.show();

            }
        });

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                saveDataToNative();
                if(resquest == 1000){
                    finish();
                    return;
                }
                startActivity(new Intent(mContext,LoginNewActivity.class));
                finish();
            }
        });

        findViewById(R.id.tv_skip).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳过,设置标记
                SPUtil.PutData(mContext,Constant.user_sex,Constant.type_qiyong);
                SPUtil.PutData(mContext,Constant.user_birthDay,Constant.type_qiyong);


                startActivity(new Intent(mContext,LoginNewActivity.class));

                finish();
            }
        });
    }

    private void saveDataToNative() {
        try {
            boolean select = man_sexview.getSelect();
            boolean select_woman = woman_sexview.getSelect();
            SPUtil.PutData(mContext,Constant.user_sex,(select? "男" : "女"));
            SPUtil.PutData(mContext,Constant.user_birthDay,user_birthDay);
            String sex = SPUtil.getDataForString(mContext, Constant.user_sex, "");
            String brith = SPUtil.getDataForString(mContext, Constant.user_birthDay, "");
            System.out.println("");
        }catch (Exception e){

        }

    }


    private void setSexClick() {
        man_sexview.getClickView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                man_sexview.setSelect(!man_sexview.getSelect());
                if(man_sexview.getSelect()){
                    woman_sexview.setSelect(false);
                }
            }
        });
        woman_sexview.getClickView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                woman_sexview.setSelect(!woman_sexview.getSelect());
                if(woman_sexview.getSelect()){
                    man_sexview.setSelect(false);
                }
            }
        });
    }

    private TimePickerView pvTime;
    private void initTimePicker() {//Dialog 模式下，在底部弹出
        pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {

                user_birthDay = getTime(date);
                setBrithDay(user_birthDay);

                Toast.makeText(mContext, user_birthDay, Toast.LENGTH_SHORT).show();
                Log.i("pvTime", "onTimeSelect");

            }
        })
                .setTimeSelectChangeListener(new OnTimeSelectChangeListener() {
                    @Override
                    public void onTimeSelectChanged(Date date) {
                        Log.i("pvTime", "onTimeSelectChanged");
                    }
                })
                .setType(new boolean[]{true, true, true,false,false,false})//true, true, true, true, true, true
                .isDialog(true) //默认设置false ，内部实现将DecorView 作为它的父控件。
                .addOnCancelClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Log.i("pvTime", "onCancelClickListener");
                    }
                })
                .setItemVisibleCount(2) //若设置偶数，实际值会加1（比如设置6，则最大可见条目为7）
                .setLineSpacingMultiplier(2.0f)
                .isAlphaGradient(true)
                .build();

        Dialog mDialog = pvTime.getDialog();
        if (mDialog != null) {

            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM);

            params.leftMargin = 0;
            params.rightMargin = 0;
            pvTime.getDialogContainerLayout().setLayoutParams(params);

            Window dialogWindow = mDialog.getWindow();
            if (dialogWindow != null) {
                dialogWindow.setWindowAnimations(com.bigkoo.pickerview.R.style.picker_view_slide_anim);//修改动画样式
                dialogWindow.setGravity(Gravity.BOTTOM);//改成Bottom,底部显示
                dialogWindow.setDimAmount(0.3f);
            }
        }
    }

    private void setBrithDay(String user_birthDay) {
        if (TextUtils.isEmpty(user_birthDay))
            return;
        select_brith_day.setRightText(user_birthDay + "");
        button1.setSatate(true);
    }

    private String getTime(Date date) {//可根据需要自行截取数据显示
        Log.d("getTime()", "choice date millis: " + date.getTime());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");//yyyy-MM-dd HH:mm:ss
        return format.format(date);
    }



}